package com.lx.pc.restful.task;

import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.lx.pc.domain.vo.sms.SmsVo;
import com.lx.pc.domain.vo.wx.WeixinVo;
import com.lx.pc.restful.wx.WeiXinUtils;
import com.lx.pc.service.sms.ISmsService;
import com.lx.pc.service.wx.IWeixinService;
import com.lx.pc.utils.DateUtil;

@Component
public class WeixinToken {

	/**
	 * 微信获取token
	 */
	@Resource(name = "weixinService")
	private IWeixinService weixinService;
	
	@Resource(name = "smsService")
	private ISmsService smsService;
	
	@Scheduled(cron = "0 0 */2 * * ? ")
	//@Scheduled(cron = "*/20 * * * * ? ")
	public void orderSettlement(){
    	
    	System.out.println("每小时执行一次");
    	
    	WeixinVo weixinVo = weixinService.findByWeixin();
		try {
			JSONObject json = WeiXinUtils.getAccessToken(weixinVo);
			//获取tonken
			String access_token = json.getString("access_token");
			System.out.println("access_token------"+access_token);
			System.out.println("json------"+json);
			if (access_token != null) {
				//更新token
				weixinVo.setAccessToken(access_token);
				//更新时间
				weixinVo.setModifyTime(DateUtil.dateFormat());
				weixinService.updateWeixin(weixinVo);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	//@Scheduled(cron = "0 */5 * * * ? ")
	//@Scheduled(cron = "*/20 * * * * ? ")
	@Scheduled(cron = "0 0 */23 * * ? ")
	public void removeSms(){
		System.out.println("每天23点执行一次");
		SmsVo sms = new SmsVo();
		sms.setCreateTime(DateUtil.sysTime());
		List<SmsVo> list = smsService.findSmsList(sms);
		if (list.size()>0) {
			for (SmsVo v : list) {
				smsService.delete(v.getId(), 1L, true);
			}
			System.out.println("删除成功--------------");
		}
	}
	
	/*@ResponseBody
	@RequestMapping(value = "sendMessage")
	public String sendMessage(UserVo v,final HttpServletRequest request) throws ClientException {
		Map<String, Object> result = new HashMap<String, Object>();
		SmsVo sms = SmsMessage.sendSms(v.getTelephone());
		final HttpSession httpSession = request.getSession();
		httpSession.setAttribute("code", sms.getCode());
		httpSession.setAttribute("phone", sms.getPhone());
		try {
			// TimerTask实现5分钟后从session中删除checkCode
			final Timer timer = new Timer();
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					String yxcode1 = (String) httpSession.getAttribute("code");
					if (yxcode1 != null) {
						// 确保清除不出错
						httpSession.removeAttribute("code");
						httpSession.removeAttribute("phone");
					}
					timer.cancel();
				}
			}, 5 * 60 * 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSON.toJSONString(result);
	}*/
	
	/*final HttpSession httpSession = request.getSession();
	String phone = (String) httpSession.getAttribute("phone");
	String yxcode = (String) httpSession.getAttribute("code");
	System.out.println("phone-----" + phone);
	System.out.println("yxcode1-----" + yxcode);
	if(phone.equals(v.getPhone())) {
		if (yxcode.equals(v.getCode())) {
			UserVo userVo = userService.find(new BigInteger(userId));
			
			userVo.setTelephone(v.getPhone());
			userService.updateUser(userVo);
			
			ajaxResult.setMsg("1");
		}else {
			ajaxResult.setMsg("2");
		}
	}else {
		ajaxResult.setMsg("3");
	}*/
	
}
